package com.example.delegator;

import com.alibaba.fastjson.JSONObject;
import com.huawei.innovation.rdm.coresdk.basic.vo.RDMResultVO;
import com.huawei.innovation.rdm.delegate.config.DelegatorConfig;
import com.huawei.innovation.rdm.delegate.service.XdmTokenService;
import com.huawei.innovation.rdm.delegate.util.ResponseHandlerUtil;
import com.huawei.innovation.rdm.delegate.util.RestTemplateUtil;
import com.huawei.innovation.rdm.delegate.util.TenantUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Repository;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import java.net.URI;
import java.util.Locale;
import java.util.Map;
@Repository
public class CustomDelegatorService {
    @Autowired(
            required = false
    )
    private DelegatorConfig config;
    @Autowired(
            required = false
    )
    private XdmTokenService xdmTokenService;
    @Autowired(
            required = false
    )
    @Qualifier("xdmRestTemplate")
    private RestTemplate restTemplate;

    public Object Get(String uri,String moduleName,String methodName,Map<String, String> queryParms){
        String url = String.format(Locale.ROOT, "%s/%s/%s/%s/%s/%s", this.config.getDomain(),
                this.config.getSubAppId(), this.config.getServiceType(), uri, moduleName, methodName);
        // 使用 UriComponentsBuilder 构建 URL，添加参数
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
        for (Map.Entry<String, String> entry:
        queryParms.entrySet()){
            String key = entry.getKey();
            String value = entry.getValue();
            builder.queryParam(key,value);
        }
        // 构建最终的 URL
        URI finalURL = builder.build().encode().toUri();
        //构建请求头
        Map<String, String> headers = RestTemplateUtil.getHeadersMap(this.config.getDomain(), "application/json;charset=utf8");
        headers.put("X-Auth-Token", this.xdmTokenService.getToken());
        headers.put("tenantId", String.valueOf(TenantUtil.getTenantId()));
        HttpHeaders requestHeader = RestTemplateUtil.buildRequestHeader(headers);
        HttpEntity<String> requestEntity = new HttpEntity<>(requestHeader);
        //发送请求
        ResponseEntity responseEntity = restTemplate.exchange(finalURL, HttpMethod.GET,requestEntity,Object.class);
        //处理响应
        RDMResultVO rdmResultVO = ResponseHandlerUtil.handleReturnValue(responseEntity.getBody());
        return rdmResultVO.getData();
    }
    public Object Post(String uri, String moduleName, String methodName, JSONObject jsonObject){
        String url = String.format(Locale.ROOT, "%s/%s/%s/%s/%s/%s", this.config.getDomain(),
                this.config.getSubAppId(), this.config.getServiceType(), uri, moduleName, methodName);
        //构建请求头
        Map<String, String> headers = RestTemplateUtil.getHeadersMap(this.config.getDomain(), "application/json;charset=utf8");
        headers.put("X-Auth-Token", this.xdmTokenService.getToken());
        headers.put("tenantId", String.valueOf(TenantUtil.getTenantId()));
        HttpHeaders requestHeader = RestTemplateUtil.buildRequestHeader(headers);
        // 设置请求体
        String jsonRequestBody = jsonObject.toString();
        HttpEntity<String> requestEntity = new HttpEntity<>(jsonRequestBody,requestHeader);
        //发送请求
        ResponseEntity responseEntity = restTemplate.postForEntity(url,requestEntity,Object.class);
        //处理响应
        RDMResultVO rdmResultVO = ResponseHandlerUtil.handleReturnValue(responseEntity.getBody());
        return rdmResultVO.getData();
    }
}
